//
//  TwERPVehSurfaceView.m
//  TwERP
//
//  Created by Xiangyang Li on 12-7-30.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "TwERPVehSurfaceView.h"


@interface TwERPVehSurfaceView ()
@property (nonatomic,strong) UIImage * img;

@end

@implementation TwERPVehSurfaceView
@synthesize img = _img;
@synthesize editable = _editable;
@synthesize surfaceDataSource =_surfaceDataSource;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
  //
    }
    return self;
}
- (UIImage *)img
{
    if(_img == nil) _img= [UIImage imageNamed:@"AUTO.png"];
    if(_img == nil) _img= [UIImage imageNamed:@"VehSurface"];
    return _img;

}
- (int)indexForPoint:(CGPoint)np
{
    CGFloat xunit= self.bounds.size.width/20;
    CGFloat yunit= self.bounds.size.height/26;
    
    return floor(np.y/yunit)*20+floor(np.x/xunit);  
    
}
-(CGRect)rectForIndex:(int)index 
{
    CGFloat xunit= self.bounds.size.width/20;
    CGFloat yunit= self.bounds.size.height/26;
    CGFloat x=fmod(index,20)*xunit;
    CGFloat y=floorf(index/20)*yunit;
    return CGRectMake(x, y, xunit, yunit);
}
- (void)drawCircle:(CGRect)rect withContext:(CGContextRef)context
{
    [[UIColor redColor] setFill];
    CGContextAddEllipseInRect(context,rect);
        CGContextFillPath(context);                
}
-(void)drawTriangle:(CGRect)rect withContext:(CGContextRef)context

{
    [[UIColor blueColor] setFill];
   
    CGContextMoveToPoint(context, rect.origin.x+rect.size.width/2, rect.origin.y);
    CGContextAddLineToPoint(context, rect.origin.x, rect.origin.y+rect.size.height);
    CGContextAddLineToPoint(context, rect.origin.x+rect.size.width, rect.origin.y+rect.size.height);
    CGContextClosePath(context);
       CGContextFillPath(context);
}
-(void)drawBlank:(CGRect)rect
{
    
}

- (void)doubleTap:(UITapGestureRecognizer *)gesture
{
    if (gesture.state == UIGestureRecognizerStateEnded) {

      if(!self.editable)  
          [self.surfaceDataSource checkSurface];
        
    }
}
- (void)tap:(UITapGestureRecognizer *)gesture
{

    if (gesture.state == UIGestureRecognizerStateEnded && self.editable) {
        CGPoint np = [gesture locationInView:self];
        int pindex = [self indexForPoint:np];
       // CGRect rect = [self rectForIndex:pindex];
        unichar state;  
 
        switch ([self.surfaceDataSource stateOfIndex:pindex]) {
            case '0':
                state = '1';
       //        [self drawTriangle:rect];
                break;
            case '1':
                state = '2';
        //       [self drawCircle:rect];
                break;
            case '2':
                state = '0';
        //        [self drawBlank:rect];
                break;
            default:
                state = '1';
                break;
        }
        
        [self.surfaceDataSource setStateOfIndex:pindex withValue:state];
        //[self setNeedsDisplayInRect:rect];
        
        [self setNeedsDisplay];
        [self.surfaceDataSource updateOtherSurfaceView];
    }
}
-(void) drawAtIndex:(int)pindex withContext:(CGContextRef)context
{
    CGRect rect = [self rectForIndex:pindex];

    switch ([self.surfaceDataSource stateOfIndex:pindex]) {
        case '1':
            [self drawTriangle:rect withContext:context];
            break;
        case '2':
            [self drawCircle:rect withContext:context];
            break;
        default:
            break;
    }
    
 
}
- (void)addGestures
{
    // enable pinch gestures in the FaceView using its pinch: handler
    [self addGestureRecognizer:[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)]];
    // recognize a pan gesture and modify our Model
   
    UITapGestureRecognizer *tapGesture1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
    tapGesture1.numberOfTapsRequired = 2;
    [self addGestureRecognizer:tapGesture1];
    
    
    UITapGestureRecognizer *tapGesture2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];

    [self addGestureRecognizer:tapGesture2];

    
}
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    //get pixels x range
    //iterate each x,get y, draw poing (x,y)
    //draw text of program
    
    [self.img drawInRect:self.bounds];
    
    CGContextRef context = UIGraphicsGetCurrentContext();
  
     
    CGContextBeginPath(context);
             
  //  CGContextSetLineWidth(context, 1.0);
    [[UIColor blueColor] setStroke];
    for (int i=0; i<520; i++) {
        [self drawAtIndex:i withContext:context];
    }
 
    
    //CGContextStrokePath(context);
 
    
    //show program description
    
    
}

@end
